"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 主/备节点故障-DN主+DN备故障-kill -9杀掉圈内dn主备进程
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    步骤 1.启动数据库
    步骤 2.运行tpcc程序
    步骤 3.kill掉主机和备机数据库进程
    步骤 4.查看数据库状态
    步骤 5.运行tpcc程序
    步骤 6.重启数据库，等待主备同步
    步骤 7.查看数据库状态
    步骤 8.运行tpcc程序
Expect      :
    步骤 1.启动数据库正常
    步骤 2.运行tpcc程序正常
    步骤 3.kill掉主备成功
    步骤 4.主备stop
    步骤 5.运行tpcc程序，无法连接数据库
    步骤 6.重启数据库，等待主备同步成功
    步骤 7.主备normal
    步骤 8.运行tpcc程序正常
History     :
    modified: 2021/01/11  modified by @wan005,tearDown 中增加对数据库状态的查询，帮助定位
    modified: 2021/01/13  modified by @peilinqian，修改重启数据库断言，增加同步等待；
    原因为：执行过tpcc后，直接kill会导致主备数据库同步状态不一致，备机为needrepair的情况
"""
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '-----Opengauss_Reliability_Dbsys_Case009.py start-----')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()
        self.TPCC_PATH = macro.TPCC_PATH

    def test_startdb(self):
        self.log.info('运行tpcc程序')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh '
                                    f'{macro.TPCC_PROPS_FILENAME}')
        self.log.info(result)
        self.assertTrue(result.find('Session End') > -1)

        self.log.info('kill掉主备')
        msg = self.com.kill_pid(self.primary_root, 9)
        self.assertEqual(msg, '')

        msg = self.com.kill_pid(self.standby1_root, 9)
        self.assertEqual(msg, '')

        self.log.info('查看数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        self.log.info(status)
        self.assertFalse(status)

        self.log.info('运行tpcc程序')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh '
                                    f'{macro.TPCC_PROPS_FILENAME}')
        self.log.info(f'tpcc运行结果{result}')
        self.assertTrue(result.find('refused.') > -1)

        self.log.info('----------重启数据库----------')
        self.sh_primy.startDbCluster()

        self.log.info('----查看主机query，同步是否正常----')
        self.sh_primy.check_location_consistency('primary', 3)

        self.log.info('查看数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        self.log.info(status)
        self.assertTrue(status)

        self.log.info('运行tpcc程序')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh '
                                    f'{macro.TPCC_PROPS_FILENAME}')
        self.log.info(result)
        self.assertTrue(result.find('Session End') > -1)

    def tearDown(self):
        self.log.info('--------------this is tearDown--------------')

        self.log.info('----查看主机query，同步是否正常----')
        self.sh_primy.check_location_consistency('primary', 3)

        self.log.info('----Opengauss_Reliability_Dbsys_Case009.py finish----')
